2020-2021 Sunseeker Telemetry and Lighting System
dma_ex4_singleTransferADC12.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
66 //******************************************************************************
67 
68 #include "driverlib.h"
69 
70 //ADC conversion result is stored in this variable
71 uint16_t DMA_DST;
72 
73 void main (void)
74 {
75  //Stop Watchdog Timer
76  WDT_A_hold(WDT_A_BASE);
77 
78  //Clear P1.0 LED off
80  GPIO_PORT_P1,
81  GPIO_PIN0
82  );
83 
84  //Set P1.0 to output direction
85  GPIO_setAsOutputPin(
86  GPIO_PORT_P1,
87  GPIO_PIN0
88  );
89 
90  //P5.7/TB1 option select Output direction
91  GPIO_setAsPeripheralModuleFunctionOutputPin(
92  GPIO_PORT_P5,
93  GPIO_PIN7
94  );
95  //Enable A/D channel A0
96  GPIO_setAsPeripheralModuleFunctionInputPin(
97  GPIO_PORT_P6,
98  GPIO_PIN0
99  );
100 
101  //Set up Timer A
102  Timer_A_initCompareModeParam initCompParam = {0};
103  initCompParam.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_1;
104  initCompParam.compareInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE;
105  initCompParam.compareOutputMode = TIMER_A_OUTPUTMODE_RESET_SET;
106  initCompParam.compareValue = 0x8000;
107  Timer_A_initCompareMode(TIMER_A0_BASE, &initCompParam);
108 
109  //Start timer A0
110  Timer_A_initUpDownModeParam initUpDownParam = {0};
111  initUpDownParam.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
112  initUpDownParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
113  initUpDownParam.timerPeriod = 0xFFFE;
114  initUpDownParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
115  initUpDownParam.captureCompareInterruptEnable_CCR0_CCIE =
116  TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
117  initUpDownParam.timerClear = TIMER_A_DO_CLEAR;
118  initUpDownParam.startTimer = true;
119  Timer_A_initUpDownMode(TIMER_A0_BASE, &initUpDownParam);
120 
121  //Initialize and setup the ADC12 Module
122  /*
123  * Base Address of the ADC12 Module
124  * Use internal ADC12 bit as sample/hold signal to start conversion
125  * USE MODOSC 5MHZ Digital Oscillator as clock source
126  * Use default clock divider of 1
127  */
128  ADC12_A_init(ADC12_A_BASE,
129  ADC12_A_SAMPLEHOLDSOURCE_3,
130  ADC12_A_CLOCKSOURCE_ADC12OSC,
131  ADC12_A_CLOCKDIVIDER_1);
132 
133  ADC12_A_enable(ADC12_A_BASE);
134  /*
135  * Base Address of the ADC12 Module
136  * For memory buffers 0-7 sample/hold for 256 clock cycles
137  * For memory buffers 8-15 sample/hold for 4 clock cycles (default)
138  * Enable Multiple Sampling
139  */
140  ADC12_A_setupSamplingTimer(ADC12_A_BASE,
141  ADC12_A_CYCLEHOLD_128_CYCLES,
142  ADC12_A_CYCLEHOLD_4_CYCLES,
143  ADC12_A_MULTIPLESAMPLESENABLE);
144 
145  //Configure Memory Buffer
146  /*
147  * Base Address of the ADC12 Module
148  * Configure memory buffer 0
149  * Map input A0 to memory buffer 0
150  * Vref+ = AVcc
151  * Vref- = AVss
152  * Memory buffer 0 is not the end of a sequence
153  */
154  ADC12_A_configureMemoryParam configureMemoryParam = {0};
155  configureMemoryParam.memoryBufferControlIndex = ADC12_A_MEMORY_0;
156  configureMemoryParam.inputSourceSelect = ADC12_A_INPUT_A0;
157  configureMemoryParam.positiveRefVoltageSourceSelect = ADC12_A_VREFPOS_AVCC;
158  configureMemoryParam.negativeRefVoltageSourceSelect = ADC12_A_VREFNEG_AVSS;
159  configureMemoryParam.endOfSequence = ADC12_A_NOTENDOFSEQUENCE;
160  ADC12_A_configureMemory(ADC12_A_BASE ,&configureMemoryParam);
161 
162 
163  //Enable/Start sampling and conversion
164  /*
165  * Base Address of the ADC12 Module
166  * Start the conversion into memory buffer 0
167  * Use the single-channel, single-conversion mode
168  */
169  ADC12_A_startConversion(ADC12_A_BASE,
170  ADC12_A_MEMORY_0,
171  ADC12_A_REPEATED_SINGLECHANNEL);
172 
173  //Disable the DMA from preempting a Read-Modify-Write Operation in the CPU
174  DMA_disableTransferDuringReadModifyWrite();
175 
176  //Initialize and Setup DMA Channel 0
177  /*
178  * Configure DMA channel 0
179  * Configure channel for repeated single transfers
180  * DMA interrupt flag will be set after every 1 transfer
181  * Use DMA Trigger Source 24 (ADC12xIFG)
182  * Transfer Word-to-Word
183  * Trigger upon Rising Edge of Trigger Source Signal
184  */
185  DMA_initParam initParam = {0};
186  initParam.channelSelect = DMA_CHANNEL_0;
187  initParam.transferModeSelect = DMA_TRANSFER_REPEATED_SINGLE;
188  initParam.transferSize = 1;
189  initParam.triggerSourceSelect = DMA_TRIGGERSOURCE_24;
190  initParam.transferUnitSelect = DMA_SIZE_SRCWORD_DSTWORD;
191  initParam.triggerTypeSelect = DMA_TRIGGER_RISINGEDGE;
192  DMA_init(&initParam);
193  /*
194  * Configure DMA channel 0
195  * Use ADC12 Memory Buffer 0 as source
196  * Don't move the source address after every transfer
197  */
198  DMA_setSrcAddress(DMA_CHANNEL_0,
199  ADC12_A_getMemoryAddressForDMA(ADC12_A_BASE,
200  ADC12_A_MEMORY_0),
201  DMA_DIRECTION_UNCHANGED);
202  /*
203  * Configure DMA channel 0
204  * Use DMA_DST as destination
205  * Increment destination address after every transfer
206  */
207  DMA_setDstAddress(DMA_CHANNEL_0,
208  (uint32_t)(uintptr_t)&DMA_DST,
209  DMA_DIRECTION_INCREMENT);
210 
211  //Enable DMA channel 0 interrupt
212  DMA_clearInterrupt(DMA_CHANNEL_0);
213  DMA_enableInterrupt(DMA_CHANNEL_0);
214 
215  //Enable transfers on DMA channel 0
216  DMA_enableTransfers(DMA_CHANNEL_0);
217 
218  //LPM0 w/ interrupts
219  __bis_SR_register(LPM0_bits + GIE);
220  //For Debugger
221  __no_operation();
222 }
223 
224 //------------------------------------------------------------------------------
225 //DMA Interrupt Service Routine
226 //------------------------------------------------------------------------------
227 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
228 #pragma vector=DMA_VECTOR
229 __interrupt
230 #elif defined(__GNUC__)
231 __attribute__((interrupt(DMA_VECTOR)))
232 #endif
233 void DMA_ISR (void)
234 {
235  switch (__even_in_range(DMAIV,16)){
236  case 0: break;
237  case 2: //DMA0IFG = DMA Channel 0
238  //Toggle P1.0 - PLACE BREAKPOINT HERE AND CHECK DMA_DST VARIABLE
239  GPIO_toggleOutputOnPin(
240  GPIO_PORT_P1,
241  GPIO_PIN0
242  );
243  break;
244  case 4: break; //DMA1IFG = DMA Channel 1
245  case 6: break; //DMA2IFG = DMA Channel 2
246  case 8: break; //DMA3IFG = DMA Channel 3
247  case 10: break; //DMA4IFG = DMA Channel 4
248  case 12: break; //DMA5IFG = DMA Channel 5
249  case 14: break; //DMA6IFG = DMA Channel 6
250  case 16: break; //DMA7IFG = DMA Channel 7
251  default: break;
252  }
253 }
254 
void DMA_ISR(void)
void main(void)
uint16_t DMA_DST
__no_operation()
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)